package com.himilco.ocana.workflow.core.bo.sm;

import com.himilco.ocana.workflow.core.bo.MailManager;
import com.himilco.ocana.workflow.core.bo.OperatorBO;
import com.himilco.ocana.workflow.core.bo.model.OperatorInterventionStateBean;
import com.himilco.ocana.workflow.core.dao.entities.Ticket;
import java.io.Serializable;
import java.util.Date;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import org.drools.KnowledgeBase;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderError;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.logger.KnowledgeRuntimeLoggerFactory;
import org.drools.runtime.StatefulKnowledgeSession;

@Stateless
public class OperatorInterventionClient implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = 6619613562049039249L;
    private static String DLRFileAssociated = "OperatorIntervention.drl";
    @EJB
    private OperatorBO operatorBO;

    /*
     * this method englobes processing for Ready to notify State and Service
     * Notified State
     */
    public void invokeOperatorChoiceRules(String operatorChoice, Ticket iTicket) {
        try {

            StatefulKnowledgeSession ksession = createKnowledgeSession();
            KnowledgeRuntimeLoggerFactory.newConsoleLogger(ksession);
            //choosing the right status for ticket depending on operator choice
            if ("DISPATCH".equals(operatorChoice)) {
                iTicket.setStatus("READY_TO_NOTIFY");
                this.processDispatchOpChoice(ksession, iTicket);
            } else if ("DIFFER".equals(operatorChoice)) {
                iTicket.setStatus("DIFFERED");
                this.processDifferOpChoice(ksession, iTicket);
            } else {
                //CPCOM CHOICE
                iTicket.setStatus("AUTOMATION_IN_PROGRESS");
                this.processNcpcomOpChoice(ksession, iTicket);
            }

        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

    public void processDispatchOpChoice(StatefulKnowledgeSession ksession, Ticket iTicket) {
        //injecting ticket on rule engine to dispatch msg
        ksession.insert(iTicket);
        MailManager mailManager = new MailManager();
        ksession.insert(mailManager);
        OperatorInterventionStateBean oi = new OperatorInterventionStateBean();
        oi.setCheckCashTask(true);
        oi.setRestartTask(true);
        ksession.insert(oi);
        ksession.fireAllRules();
        //updating ticket status after dispatch
        iTicket.setStatus("DISPATCHED");
        getOperatorBO().updateTicket(iTicket);
    }

    public void processNcpcomOpChoice(StatefulKnowledgeSession ksession, Ticket iTicket) {
        ksession.insert(iTicket);
//        MailManager mailManager = new MailManager();
//        ksession.insert(mailManager);
        OperatorInterventionStateBean oi = new OperatorInterventionStateBean();
        oi.setCheckCashTask(true);
        oi.setRestartTask(true);
        ksession.insert(oi);
        ksession.fireAllRules();
        //updating ticket status after dispatch
        iTicket.setStatus("AUTOMATION_IN_PROGRESS");
        getOperatorBO().updateTicket(iTicket);
    }

    public void processDifferOpChoice(StatefulKnowledgeSession ksession, Ticket iTicket) {
        ksession.insert(iTicket);
//        MailManager mailManager = new MailManager();
//        ksession.insert(mailManager);
//        OperatorInterventionStateBean oi = new OperatorInterventionStateBean();
//        oi.setCheckCashTask(true);
//        oi.setRestartTask(true);
//        ksession.insert(oi);
        ksession.fireAllRules();
        //updating ticket status after dispatch
        iTicket.setStatus("DIFFERED");
        getOperatorBO().updateTicket(iTicket);
    }

    private static StatefulKnowledgeSession createKnowledgeSession() {
        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
        kbuilder.add(
                ResourceFactory.newClassPathResource(getDLRFileAssociated()),
                ResourceType.DRL);
        if (kbuilder.hasErrors()) {
            if (kbuilder.getErrors().size() > 0) {
                for (KnowledgeBuilderError kerror : kbuilder.getErrors()) {
                    System.err.println(kerror);
                }
            }
        }

        KnowledgeBase kbase = kbuilder.newKnowledgeBase();
        StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
        return ksession;
    }

    public static String getDLRFileAssociated() {
        return DLRFileAssociated;
    }

    public void setDLRFileAssociated(String dLRFileAssociated) {
        DLRFileAssociated = dLRFileAssociated;
    }

    public OperatorBO getOperatorBO() {
        return operatorBO;
    }
}
